<!--
 * @Author: YU
 * @Date: 2021-10-24 13:56:24
 * @LastEditTime: 2021-10-25 21:09:13
 * @LastEditors: YU
 * @Description: 登录页面
-->
<template>
  <div class="login">
    <div class="login_box">
      <!-- 头像开始 -->
      <div class="box_pic">
        <img src="../assets/logo.png" />
      </div>
      <!-- 头像结束 -->
      <!-- 表单开始 -->
      <div class="box_form">
        <el-form ref="loginRef" :model="form" :rules="loginFormRules" class="form_ipt">
          <el-form-item prop="username">
            <el-input v-model="form.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" prefix-icon="iconfont icon-3702mima" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item class="ipt_btns">
            <el-button type="primary" @click="loginForm()" :loading="loadingLogin">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表单结束 -->
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login'
export default {
  data() {
    return {
      form: {
        username: '', // 用户名
        password: ''// 密码
      },
      // 登录表单校验
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      },
      loadingLogin: false // 登录按钮加载效果
    }
  },
  methods: {
    /**
     * @description:登录
     * @param {*}
     * @return {*}
     */
    loginForm() {
      this.$refs.loginRef.validate(async(valid) => {
        if (!valid) return false
        this.loadingLogin = true
        const { data: res } = await login(this.form)
        if (res.meta.status === 200) {
          this.$store.commit('setUser', res.data.token)
          this.$message.success('登录成功')
          this.$router.push('/home')
        } else if (res.meta.status === 400) {
          this.$message.error('用户名或者密码错误!')
        } else {
          this.$message.error('登录失败!')
        }
        this.loadingLogin = false
      })
    },

    /**
     * @description:重置
     * @param {*}
     * @return {*}
     */
    resetForm() {
      this.$refs.loginRef.resetFields()
    }
  }
}
</script>

<style lang='scss' scoped>
.login {
  height: 100vh;
  width: 100vw;
  background-color: #2b4b6b;
  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 300px;
    background-color: #fff;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    .box_pic {
      position: absolute;
      left: 50%;
      width: 130px;
      height: 130px;
      border-radius: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      background-color: #fff;
      border: 1px solid #f0f0f0;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eeeeee;
      }
    }

    .box_form {
      box-sizing: border-box;
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      .ipt_btns{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
